import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_switch/flutter_switch.dart';
import 'package:lcapp/api/interface/home_api.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/data/xian_huo_data_entity.dart';
import 'package:lcapp/model/dialog/confirm.dart';
import 'package:lcapp/tools/string_tool.dart';
import 'package:lcapp/widget/asr_cache_image.dart';

class MinXianHuoItm extends StatefulWidget {
  final XianHuoDataEntity entity;
  final Function editClick;
  const MinXianHuoItm({Key? key, required this.entity, required this.editClick}) : super(key: key);

  @override
  _MinXianHuoItmState createState() => _MinXianHuoItmState();
}

class _MinXianHuoItmState extends State<MinXianHuoItm> {

  late String location;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if(widget.entity.address!=null&&widget.entity.address!.isNotEmpty){
      List arr = widget.entity.address!.split(' ');
      print('arr====='+arr.toString());
      if(arr.length>=4){
        location = '${arr[0]??''} ${arr[1]??''} ${arr[2]??''}';
      }else{
        location = widget.entity.address!;
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        Navigator.of(context).pushNamed('XianHuoDetailPage',arguments: widget.entity.id);
      },
      child: Container(
        margin: EdgeInsets.only(top: 20.w),
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(10.w)
        ),
        padding: EdgeInsets.symmetric(horizontal: 30.w,vertical: 30.w),
        child: Column(
          children: [
            Container(
              child: Row(
                children: [
                 Visibility(child:  Container(
                   color: bgColor,
                   child: ClipRRect(
                     child: ASRCacheImage(
                       width: 160.w,
                       height: 160.w,
                       image: widget.entity.images!=null&&widget.entity.images!.isNotEmpty?widget.entity!.images![0]:'',
                     ),
                     borderRadius: BorderRadius.circular(10.w),
                   ),
                 ),visible: widget.entity.images!=null&&widget.entity.images!.isNotEmpty,),
                  SizedBox(width: 20.w,),
                  Expanded(child: Container(
                    height: 160.w,
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Expanded(child: RichText(
                          maxLines: 2,
                          softWrap: true,
                          text: TextSpan(
                            children: [
                              WidgetSpan(child: Container(
                                child: Text(widget.entity.year!,style: TextStyle(
                                  fontSize: 24.w,
                                  color: Colors.white,
                                ),),
                                margin: EdgeInsets.only(right: 10.w),
                                padding: EdgeInsets.symmetric(vertical: 6.w,horizontal: 16.w),
                                decoration: BoxDecoration(
                                    color: Color(0xffEB6E30),
                                    borderRadius: BorderRadius.circular(6.w)
                                ),
                              )),
                              TextSpan(
                                text: widget.entity.name??'',
                                style: TextStyle(
                                    fontSize: 34.w,
                                    color: Colors.black,
                                    fontWeight: FontWeight.w600
                                ),
                              )
                            ],
                          ),
                          textAlign: TextAlign.left,
                        )),
                        Container(
                            child: Row(
                              children: [
                                Expanded(
                                    child: Container(
                            child: Text(location??'',maxLines: 1,style: TextStyle(
                                color:Color(0xff666666),
                                fontSize: 24.sp
                            ),),
                        )
                                ),
                                Text('库存：${widget.entity.numbers}吨',style: TextStyle(
                                    color:Color(0xff333333),
                                    fontSize: 24.sp
                                ),),
                              ],
                            )
                        ),
                        SizedBox(height: 10.w,),
                        Container(
                          height: 1,
                          color: Color(0xffE9E8E9),
                        ),
                      ],
                    ),
                  ))
                ],
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 40.w),
              alignment: Alignment.centerRight,
              child:  RichText(
                maxLines: 1,
                softWrap: true,
                text: TextSpan(
                  text: '￥${widget.entity.price}',
                  style: TextStyle(
                      fontSize: 30.w,
                      color: const Color(0xffFF1818),
                      fontWeight: FontWeight.w600
                  ),
                  children: <TextSpan>[
                    TextSpan(
                      text: ' ${StringTool.getDWstr(widget.entity.productUnit!)}',
                      style:  TextStyle(
                        color: Color(0xff333333),
                        fontSize: 24.sp,
                      ),
                    ),
                  ],
                ),
                textAlign: TextAlign.left,
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 30.w),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Expanded(child: Container(
                    child: Row(
                      children: [
                        Container(
                          child: Text('上架:',style: TextStyle(
                            color: Colors.black,
                            fontSize: 24.sp,
                            fontWeight: FontWeight.w500
                          ),),
                          margin: EdgeInsets.only(right: 20.w),
                        ),
                        FlutterSwitch(
                          width: 90.w,
                          height: 50.w,
                          padding: 1,
                          value: widget.entity.status==1,
                          activeColor:Color(0xff07C160),
                          inactiveColor: Color(0xffeeeeee),
                          onToggle: (value) async{
                            FocusScope.of(context).requestFocus(FocusNode());
                            ResponseData res = await HomeAPI.changeXianhuoStatus(widget.entity.id!, widget.entity.status==1?0:1);
                            if(res.code==200){
                              setState(() {
                                widget.entity.status  = widget.entity.status==1?0:1;
                              });
                            }
                          },
                        )
                      ],
                    ),
                  )),
                  SizedBox(width: 20.w,),
                  Visibility(child: GestureDetector(
                    onTap: (){
                      openConfirm(
                        context,
                        isTitle: true,
                        title: '提示',
                        content: '确定删除该条作品吗',
                        succFn: () {
                          if(widget.editClick!=null){
                            widget.editClick(0);
                          }
                        },
                      );
                    },
                    child: Container(
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Image.asset('assets/xh/xh_delete.png',width: 28.w,),
                          SizedBox(width: 6.w,),
                          Container(
                            padding: EdgeInsets.only(top: 5.w),
                            child: Text('删除',style: TextStyle(
                                color:Color(0xff666666),
                                fontSize: 28.sp
                            ),),
                          )
                        ],
                      ),
                    ),
                  ),visible: true,),
                  SizedBox(width: 20.w,),
                  GestureDetector(
                    onTap: (){
                      if(widget.editClick!=null){
                        widget.editClick(1);
                      }
                    },
                    child: Container(
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Image.asset('assets/xh/xh_bj.png',width: 28.w,),
                          SizedBox(width: 6.w,),
                          Container(
                            padding: EdgeInsets.only(top: 5.w),
                            child: Text('编辑',style: TextStyle(
                                color:Color(0xff666666),
                                fontSize: 28.sp
                            ),),
                          )
                        ],
                      ),
                    ),
                  ),
                  SizedBox(width: 20.w,),
                  GestureDetector(
                    onTap: (){
                      if(widget.editClick!=null){
                        widget.editClick(2);
                      }
                    },
                    child: Container(
                      width: 180.w,
                      height: 60.w,
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                          color: Color.fromRGBO(235, 110, 48, 0.3),
                          borderRadius: BorderRadius.circular(60.w)
                      ),
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(Icons.refresh_outlined,color: Color(0xffEB6E30),size: 40.w,),
                          Text('更新价格',style: TextStyle(
                              color:Color(0xffEB6E30),
                              fontSize: 28.sp
                          ))
                        ],
                      ),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
